// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'dart:convert';

class BaseRespModel<T> {
  int code;
  String msg;
  T? result;
  BaseRespModel({
    required this.code,
    required this.msg,
    this.result,
  });

  BaseRespModel<T> copyWith({
    int? code,
    String? msg,
    T? result,
  }) {
    return BaseRespModel<T>(
      code: code ?? this.code,
      msg: msg ?? this.msg,
      result: result ?? this.result,
    );
  }

  Map<String, dynamic> toMap({dynamic Function(T)? dataToMap}) {
    return <String, dynamic>{
      'code': code,
      'msg': msg,
      'result': result == null
          ? null
          : dataToMap == null
              ? result
              : dataToMap(result as T),
    };
  }

  factory BaseRespModel.fromMap(Map<String, dynamic> map,
      {T Function(dynamic)? dataFromMap}) {
    return BaseRespModel<T>(
      code: map['code'] as int,
      msg: map['msg'] as String,
      result: map['result'] != null
          ? dataFromMap == null
              ? map["result"]
              : dataFromMap(map["result"])
          : null,
    );
  }

  String toJson(dynamic Function(T)? dataToMap) =>
      json.encode(toMap(dataToMap: dataToMap));

  factory BaseRespModel.fromJson(String source,
          {T Function(dynamic)? dataFromMap}) =>
      BaseRespModel.fromMap(json.decode(source) as Map<String, dynamic>,
          dataFromMap: dataFromMap);

  @override
  String toString() => 'BaseRespModel(code: $code, msg: $msg, result: $result)';

  @override
  bool operator ==(covariant BaseRespModel<T> other) {
    if (identical(this, other)) return true;

    return other.code == code && other.msg == msg && other.result == result;
  }

  @override
  int get hashCode => code.hashCode ^ msg.hashCode ^ result.hashCode;
}
